home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1997 / HAM Radio 1997.iso / vcls / redistry / regplace.pas < prev   
Pascal/Delphi Source File  |  1996-04-08  |  9KB  |  212 lines

  1. unit RegPlace;
  2.  
  3. (*
  4. ================================================================================
  5. TRegPlacement for Windows95 version 1.2 by marc hoffman of elite!developments
  6. largely based upon:
  7.  
  8.   TPlacement Component version 1.0 by Barry L. Harkness - Public Domain
  9.  
  10.            Feel free to use this component in any application, in
  11.            any context, without any warranty.  This component is
  12.            donated to the public domain to help fellow Delphi
  13.            programmers dominate windows software development.
  14.  
  15. this advanced version of TPlacement works practically the same as TPlacement, ecxept
  16. the placement of the window will not be saved in an .INI file, but in the system
  17. registry. for this component to run, you will also need TRegistry, also written by me,
  18. which is available as .DCU file, freeware.
  19. (and should be in the same package as this file. if this is NOT the case please inform
  20.  me, and you will get a copy of the complete package per email.)
  21.  
  22. if you have any suggestions, cannot find Registry.DCU, or whatever, please contact me at
  23. marc@arb-phys.uni-dortmund.de (marc hoffman)
  24.  
  25. one again thanx to Barry for supplying the original TPlacement component.
  26.  
  27.  
  28.  
  29.  
  30. you must include a TRegistry component on your Form and select it in the "Registry" property
  31. of TRegPlacement.
  32. the Style property states, whether the window placement is stored differently for each user logged
  33. onto th network, or in general for all users (psByUser vs. psByMachine)
  34. see the docu to TRegistry for in depth information about this.
  35.  
  36. in order to use TRegPlacement, you should, as you did with TPlacement, call the Read method when
  37. your form is created (i.e. in the OnFormCreate event) and the Write method when the form is closed
  38. or destroyed.
  39.  
  40. and remember : the Registry component works only for Win95, and so does this one, since it
  41. strongly depends on on TRegistry. (WinNT sould do the job as well, but haven't checked this.)
  42.  
  43.  
  44. public domain
  45. *)
  46.  
  47. {-------------------------------------------------------------------------------------------}
  48. interface
  49. {-------------------------------------------------------------------------------------------}
  50.  
  51. uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs,
  52.      Registry;
  53.  
  54. type TStyle = (psByUser,psByMachine);
  55.  
  56.      TRegPlacement = class(TComponent)
  57.      private
  58.        fRegistry:TRegistry;
  59.        fStyle:TStyle;
  60.        fKey:string;
  61.        procedure SetKey(s:string);
  62.      public
  63.        constructor Create(AOwner : TComponent); override;
  64.        procedure Write; virtual;
  65.        procedure Read; virtual;
  66.      published
  67.        { Published declarations }
  68.        property Regsitry:TRegistry read fRegistry write fRegistry;
  69.        property Key:string read fKey write SetKey;
  70.        property Style:TStyle read fStyle write fStyle;
  71.      end;
  72.  
  73. procedure Register;
  74.  
  75. {-------------------------------------------------------------------------------------------}
  76. implementation
  77. {-------------------------------------------------------------------------------------------}
  78.  
  79. const
  80.   pcFlags    = '\flags';
  81.   pcShowCmd  = '\ShowCmd';
  82.   pcMinPosX  = '\MinPosX';
  83.   pcMinPosY  = '\MinPosY';
  84.   pcMaxPosX  = '\MaxPosX';
  85.   pcMaxPosY  = '\MaxPosY';
  86.   pcNormPosL = '\NormPosLeft';
  87.   pcNormPosT = '\NormPosTop';
  88.   pcNormPosR = '\NormPosRight';
  89.   pcNormPosB = '\NormPosBottom';
  90.  
  91. function IntVal(s:string):integer;
  92. var i,c : integer;
  93. begin
  94.     Val(s,i,c);
  95.   result := i;
  96. end;
  97.  
  98. function IntStr(i:integer):string;
  99. var s: string;
  100. begin
  101.   Str(i,s);
  102.   result := s;
  103. end;
  104.  
  105. {-------------------------------------------------------------------------------------------}
  106. constructor TRegPlacement.Create(AOwner : TComponent);
  107. begin
  108.   inherited Create(AOwner);
  109.   Key := 'MyWindowPlacement';
  110. end;
  111.  
  112. {-------------------------------------------------------------------------------------------}
  113. procedure TRegPlacement.Read;
  114. var Placement : TWindowPlacement;
  115. begin
  116.   if Regsitry = nil then exit;
  117.   try
  118.     Placement.length :=SizeOf(TWindowPlacement);
  119.     with Placement do begin
  120.       case Style of
  121.         psByUser:begin
  122.                    Flags := IntVal(fRegistry.ReadStringUser(Key+pcFlags));
  123.                    ShowCmd := IntVal(fRegistry.ReadStringUser(Key+pcShowCmd));
  124.                    ptMinPosition.X := IntVal(fRegistry.ReadStringUser(Key+pcMinPosX));
  125.                    ptMinPosition.Y := IntVal(fRegistry.ReadStringUser(Key+pcMinPosY));
  126.                    ptMaxPosition.X := IntVal(fRegistry.ReadStringUser(Key+pcMaxPosX));
  127.                    ptMaxPosition.Y := IntVal(fRegistry.ReadStringUser(Key+pcMaxPosY));
  128.                    rcNormalPosition.Left :=   IntVal(fRegistry.ReadStringUser(Key+pcNormPosL));
  129.                    rcNormalPosition.Top :=    IntVal(fRegistry.ReadStringUser(Key+pcNormPosT));
  130.                    rcNormalPosition.Right :=  IntVal(fRegistry.ReadStringUser(Key+pcNormPosR));
  131.                    rcNormalPosition.Bottom := IntVal(fRegistry.ReadStringUser(Key+pcNormPosB));
  132.                  end;
  133.         else begin
  134.           Flags := IntVal(fRegistry.ReadString(Key+pcFlags));
  135.           ShowCmd := IntVal(fRegistry.ReadString(Key+pcShowCmd));
  136.           ptMinPosition.X := IntVal(fRegistry.ReadString(Key+pcMinPosX));
  137.           ptMinPosition.Y := IntVal(fRegistry.ReadString(Key+pcMinPosY));
  138.           ptMaxPosition.X := IntVal(fRegistry.ReadString(Key+pcMaxPosX));
  139.           ptMaxPosition.Y := IntVal(fRegistry.ReadString(Key+pcMaxPosY));
  140.           rcNormalPosition.Left :=   IntVal(fRegistry.ReadString(Key+pcNormPosL));
  141.           rcNormalPosition.Top :=    IntVal(fRegistry.ReadString(Key+pcNormPosT));
  142.           rcNormalPosition.Right :=  IntVal(fRegistry.ReadString(Key+pcNormPosR));
  143.           rcNormalPosition.Bottom := IntVal(fRegistry.ReadString(Key+pcNormPosB));
  144.         end;
  145.       end;
  146.       if rcNormalPosition.Right > rcNormalPosition.Left then
  147.         SetWindowPlacement(TForm(Owner).Handle, @Placement)
  148.     end;
  149.   except
  150.     on ERegistryError do;
  151.     else raise
  152.   end;
  153. end;
  154.  
  155. {-------------------------------------------------------------------------------------------}
  156. procedure TRegPlacement.Write;
  157. var Placement : TWindowPlacement;
  158. begin
  159.   if Regsitry = nil then exit;
  160.   Placement.length :=SizeOf(TWindowPlacement);
  161.   if not GetWindowPlacement(TForm(Owner).Handle, @Placement) then
  162.   begin
  163.     raise EAbort.Create('Unable to retrieve window placement');
  164.     Exit;
  165.   end;
  166.  
  167.   with Placement do begin
  168.     case Style of
  169.       psByUser:begin
  170.                  fRegistry.WriteStringUser(Key+pcFlags, IntStr(Flags));
  171.                  fRegistry.WriteStringUser(Key+pcShowCmd, IntStr(ShowCmd));
  172.                  fRegistry.WriteStringUser(Key+pcMinPosX, IntStr(ptMinPosition.X));
  173.                  fRegistry.WriteStringUser(Key+pcMinPosY, IntStr(ptMinPosition.Y));
  174.                  fRegistry.WriteStringUser(Key+pcMaxPosX, IntStr(ptMaxPosition.X));
  175.                  fRegistry.WriteStringUser(Key+pcMaxPosY, IntStr(ptMaxPosition.Y));
  176.                  fRegistry.WriteStringUser(Key+pcNormPosL, IntStr(rcNormalPosition.Left));
  177.                  fRegistry.WriteStringUser(Key+pcNormPosT, IntStr(rcNormalPosition.Top));
  178.                  fRegistry.WriteStringUser(Key+pcNormPosR, IntStr(rcNormalPosition.Right));
  179.                  fRegistry.WriteStringUser(Key+pcNormPosB, IntStr(rcNormalPosition.Bottom));
  180.                end;
  181.       else begin
  182.         fRegistry.WriteString(Key+pcFlags, IntStr(Flags));
  183.         fRegistry.WriteString(Key+pcShowCmd, IntStr(ShowCmd));
  184.         fRegistry.WriteString(Key+pcMinPosX, IntStr(ptMinPosition.X));
  185.         fRegistry.WriteString(Key+pcMinPosY, IntStr(ptMinPosition.Y));
  186.         fRegistry.WriteString(Key+pcMaxPosX, IntStr(ptMaxPosition.X));
  187.         fRegistry.WriteString(Key+pcMaxPosY, IntStr(ptMaxPosition.Y));
  188.         fRegistry.WriteString(Key+pcNormPosL, IntStr(rcNormalPosition.Left));
  189.         fRegistry.WriteString(Key+pcNormPosT, IntStr(rcNormalPosition.Top));
  190.         fRegistry.WriteString(Key+pcNormPosR, IntStr(rcNormalPosition.Right));
  191.         fRegistry.WriteString(Key+pcNormPosB, IntStr(rcNormalPosition.Bottom));
  192.       end;
  193.     end;
  194.   end;
  195. end;
  196.  
  197. {-------------------------------------------------------------------------------------------}
  198. procedure TRegPlacement.SetKey;
  199. begin
  200.   if s = '' then exit;
  201.   fKey := s;
  202. end;
  203.  
  204. {-------------------------------------------------------------------------------------------}
  205. {-------------------------------------------------------------------------------------------}
  206. procedure Register;
  207. begin
  208.   RegisterComponents('System', [TRegPlacement]);
  209. end;
  210.  
  211. end.
  212.